<?php
use	Doctrine\Common\ClassLoader,
	Doctrine\ORM\Tools\Setup,
	Doctrine\ORM\EntityManager;

class CrudclaseController extends Zend_Controller_Action
{

    public function init()
    {   /* Initialize action controller here */
        $registry = Zend_Registry::getInstance();
        $this->_em = $registry->entitymanager;
    }

    public function indexAction()
    {
        // action body
    }

    public function crearAction()
    {   $request = $this->getRequest();
        $form    = new Application_Form_Clase();

	if ($this->getRequest()->isPost()) {
            if ($form->isValid($request->getPost())) {
		$registro = $form->getValues();
                $this->persistirDatos($registro);
                return $this->_helper->redirector('listar');
            }
        }
 	$this->view->form = $form;
    }

    private function persistirDatos($registro)
    {//print_r($registro);exit;  
        $miprofesor = $this->_em 
            ->createQuery('select u from Default_Model_Profesor u Where u.id = :nro')
            ->setParameter('nro',$registro['combo_profesores'])
            ->getResult(); 
        
        //$miprofesor = new Default_Model_Profesor;
        //$miprofesor->setApellido('Don Pirulo');
      //print_r($miprofesor);exit;  
        $testEntity = new Default_Model_Clase;
        $testEntity->setNombre($registro['unnombre']);
        $testEntity->setIdprofesor($miprofesor[0]);
        //$testEntity->setIdprofesor($miprofesor);
        $this->_em->persist($testEntity);
        $this->_em->flush();
    }

    public function listarAction()
    {
        $miclase = $this->_em 
            ->createQuery('select u,a from Default_Model_Clase u join u.idprofesor a')
            ->getResult(); 
        
        Zend_View_Helper_PaginationControl::setDefaultViewPartial('paginator/items.phtml');
        $paginator = Zend_Paginator::factory($miclase);
        if ($this->_hasParam('page'))
        {
            $paginator->setCurrentPageNumber($this->_getParam('page'));
        }
    //print_r($miclase);exit;
        //$this->view->miclase = $miclase;
        $this->view->paginator = $paginator;
    }
    
    public function inscripcionesAction()
    {
        $insc = $this->_em 
            ->createQuery("select c.nombre catedra,p.apellido profe,a.apellido alu
                    from Default_Model_Clase c
                         join c.idalumno a
                        join c.idprofesor p 
                        
                        ")
            ->getResult(); 
        print_r($insc);exit;
        
        
        
        $inscripciones = $this->_em 
            //->createQuery('select c.nombre,a.apellido from Default_Model_Alumno a join a.idclase c order by c.nombre,a.apellido')
                ->createQuery('select c.nombre,a.apellido,p.apellido profe 
                    from Default_Model_Alumno a 
                        join a.idclase c 
                        join c.idprofesor p 
                        order by c.nombre,a.apellido')
            ->getResult(); 
    //print_r($inscripciones);exit;
        $this->view->inscripciones = $inscripciones;
    }


}







